﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.Domain;
using MShop.Web.Components.Extensions; 
using MShop.Core.DataAccess;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class PowersController : SecureController
    {

        private readonly ICommonDao<Powers> _CommonDao;
        private readonly ICommonDao<aspnet_Roles> _CommonDao2;
        private readonly IMenuDao _MenuDao;
        public PowersController(ICommonDao<Powers> commonDao, ICommonDao<aspnet_Roles> commonDao2, IMenuDao menuDao)
        {
            _CommonDao = commonDao;
            _CommonDao2 = commonDao2;
            _MenuDao=menuDao;
        }

        public ActionResult Index(Guid? roleid)
        {

            IList<SelectListItem> roles =_CommonDao2.GetAll().ConvertListItem("rolename", "roleid", roleid);
            ViewData["roles"] = roles;          
            ViewData["menus"]= _MenuDao.Build();
            if (roleid.HasValue)
            {                
                return View(roleid.Value);

            }
            else
            {               
               // ViewData["rights"] = typeof(RightFlags).ConvertListItem();
                return View(new Guid(roles[0].Value));
            }
            
        }

        [HttpPost]
        public ActionResult Index(Guid roleid,FormCollection collection)
        {
            foreach (var menuid in collection["MenuId"].Split(','))
            {
                string rightsid = collection["Rights" + menuid];
                int menuid2 = int.Parse(menuid);
                Powers power = _CommonDao.Find(x => x.MenuId == menuid2 && x.RoleId == roleid).SingleOrDefault();                   
                if (power == null)
                {
                    power = new Powers();
                }
                power.RoleId = roleid;
                power.MenuId = int.Parse(menuid);
                power.Rights = (int)power.GetRights(rightsid);
             
                if (power.EntityState == System.Data.EntityState.Detached)
                {
                    _CommonDao.Save(power);
                }              
            }
            _CommonDao.SaveChanges();
            return RedirectToAction("index", new { roleid = collection["RoleId"] });

        }

       
        //public ActionResult Create(Guid? roleid)
        //{
        //    ViewData["roles"] = _CommonDao2.GetAll().ConvertListItem("rolename", "roleid", roleid);
        //    ViewData["rights"] = typeof(RightFlags).ConvertListItem();
        //    if (roleid.HasValue)
        //    {
        //        ViewData["menus"] = DataContext.Menu;
        //    }
         
        //    return View();
        //}

        //[HttpPost]
        //public ActionResult Create(FormCollection collection)
        //{
        //    foreach (var menuid in collection["MenuId"].Split(','))
        //    {
        //        string rightsid = collection["Rights" + menuid];
        //        Powers power = new Powers();
        //        power.RoleId = new Guid(collection["RoleId"]);
        //        power.MenuId = int.Parse(menuid);
        //        power.Rights = (int)power.GetRights(rightsid);
        //        DataContext.AddToPowers(power);
        //    }
        //    DataContext.SaveChanges();
        //    return RedirectToAction("index"); 
            
        //}
    }
}
